package DES

// 明文 32 位扩展为 48 位
func EChange(input uint32) (output uint64) {
	for i := 0; i < 48; i++ {
		output |= uint64(input >> uint(32 - EBox[i]) & 0x1)
		output = output << 1
	}
	return output >> 1
}

// 单个S盒 变换 index 为 S 盒编号
func Single_Sbox(input uint8, index int) (output uint8) {
	i := (input & 33 & 0x1) | (input & 33) >> 4
	j := (input & 30) >> 1
	return S[index - 1][i * 16 + j]
}

func PChange(input uint32) (output uint32) {
	for i := 0; i < 32; i++ {
		output |= input >> uint(32 - P[i]) & 0x1
		output = output << 1
	}
	return output >> 1
}


func F(input uint32) (output uint32) {
	tmp := EChange(input)
	// tmp 48位通过S盒置换
	for i := 0; i < 8; i++ {
		t := (tmp >> (6 * (7 - i))) & 63
		output |= uint32(Single_Sbox(uint8(t), i))
		output <<= 4
	}
	return PChange(output)
}


